package com.java_foundation.thread_demo.test_13;

/**
 * @program: java_foundation
 * @description: ThreadLocal使用示例
 * @author: xiongbangwen <Email>xiongbangwen@163.com</Email>
 * @create: 2020-07-09 23:36
 **/
public class ThreadLocalDemo {
    static ThreadLocal<String> localVariable = new ThreadLocal<>();

    static void print(String str) {
        //打印当前线程本地内存中localVariable变量的值
        System.out.println(str + ":" + localVariable.get());
        //清除当前线程本地内存中的localVariable变量
        //localVariable.remove();
    }

    public static void main(String[] args) {
        //创建线程A
        Thread threadA = new Thread(new Runnable() {
            @Override
            public void run() {
                localVariable.set("threadA的本地变量");
                print("threadA");
                System.out.println("A remove之后的值" + localVariable.get());
            }
        });

        //创建线程B
        Thread threadB = new Thread(new Runnable() {
            @Override
            public void run() {
                localVariable.set("threadB的本地变量");
                print("threadB");
                System.out.println("B remove之后的值" + localVariable.get());
            }
        });
        threadA.start();
        threadB.start();
    }
}
